<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use yii\helpers\Json;
use app\models\Cf_picture;
use app\models\Pr_project;
use app\models\Rs_raised;
use app\models\Info_information;
use app\models\Uc_user;

class IndexController extends Controller
{
    //禁用原有布局
    public $layout="main2";
    //首页显示
    public function actionShow($raised = 'recommend')
    {


        RegisterController::prepare();



        $pictures = $this->getPicture();
        $projects = $this->getProject();
        $raiseds = $this->getRaised($raised);
        $teams = $this->getTeam();
        $informations = $this->getInformation();
        return $this->render('index',[
            'pictures' => $pictures,
            'projects' => $projects,
            'raiseds' => $raiseds,
            'teams' => $teams,
            'informations' => $informations,
            'raised' => $raised
            ]);
    }
    //广告轮播数据
    public function getPicture()
    {    
        $query = Cf_picture::find()
        ->where([
            'is_recommend' => 1,
            'del_flag' => 0
            ])
        ->orderBy('id DESC')
        ->limit(4)
        ->all();
        return $query;
    }
    //创业项目数据
    public function getProject()
    {    
        $query = Pr_project::find()
        ->where([
            'status' => [0, 1, 2],
            'is_recommend' => 1,
            'del_flag' => 0
            ])
        ->orderBy('pr_id DESC')
        ->limit(10)
        ->all();
        
        return $query;
    }
    //项目聚募数据,设置为action开头是为了ajax调用
    public function getRaised($type = 'recommend')
    {    
        //推荐聚募
        if($type == 'recommend')
        {
            $query = Rs_raised::find()
            ->where([
                'is_recommend' => 1,
                'del_flag' => 0,
                'type' => [0,1,2]
                ])
            ->limit(12);
        }   
        //最新聚募
        else if($type == 'new')
        {
            $query = Rs_raised::find()
            ->where([
                'del_flag' => 0,
                'type' => [0,1,2]
                ])
            ->limit(12);
        }
        $pagination = new Pagination([
            'defaultPageSize' => 4,
            'totalCount' => 12,
            'pageParam' => 'page1'
            ]);
        $raiseds = $query->orderBy('raised_id DESC')
        ->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();
        //匹配用户id和用户名，计算进度
        foreach ($raiseds as $key => $raised)
        {
            $query1 = Uc_user::findOne([
                'user_id' => $raised->leader_id
                ]);
            $query2 = Uc_user::findOne([
                'user_id' => $raised->referrer_id
                ]);
            if(null == $query1)
            {
             $raised->leader_name = "暂无";
         }
         else
         {
             $raised->leader_name = $query1->user_name;
         }
         if(null == $query2)
         {
             $raised->referrer_name = "暂无";
         }
         else
         {
             $raised->referrer_name = $query2->user_name;
         }
            //进度百分比，保留2位小数
         $progress = $raised->raisedMoney/$raised->totalMoney*100;
         $raised->progress = round($progress, 2);
         $raised->days = floor((strtotime($raised->expire)-time())/86400);
     }
     return [
     'raiseds' => $raiseds,
     'pagination' => $pagination
     ];
 }
    //创业团队数据
 public function getTeam()
 {    
    $query = Info_information::find()
    ->where([
        'del_flag' => 0,
            'type' => 3 //3代表团队
            ])
    ->orderBy('id')
    ->limit(5)
    ->all();
    return $query;
}
    //资讯数据
public function getInformation($type = 'activity')
{   
        //活动
        $query0 = Info_information::find()
        ->where([
            'del_flag' => 0,
                    'type' => 0 //0代表活动
                    ])
        ->limit(12);
        //新闻 
        $query1 = Info_information::find()
        ->where([
            'del_flag' => 0,
                    'type' => 1 //1代表新闻
                    ])
        ->limit(12);
        //公告
        $query2 = Info_information::find()
        ->where([
            'del_flag' => 0,
                    'type' => 2 //2代表公告
                    ])
        ->limit(12);
    // $pagination = new Pagination([
    //     'defaultPageSize' => 4,
    //     'totalCount' => 12,
    //     'pageParam' => 'page2'
    //     ]);
    $informations = array();
    $informations[0] = $query0->orderBy('id DESC')
    ->offset(4)
    ->limit(4)
    ->all();
    $informations[1] = $query1->orderBy('id DESC')
    ->offset(4)
    ->limit(4)
    ->all();
    $informations[2] = $query2->orderBy('id DESC')
    ->offset(4)
    ->limit(4)
    ->all();
    return [
    'informations' => $informations
    ];
}

public function actionRaised($type = 'recommend', $page = 1)
    {    
        //推荐聚募
        if($type == 'recommend')
        {
            $query = Rs_raised::find()
            ->where([
                'is_recommend' => 1,
                'del_flag' => 0,
                'type' => [0,1,2]
                ])
            ->limit(12);
        }   
        //最新聚募
        else if($type == 'new')
        {
            $query = Rs_raised::find()
            ->where([
                'del_flag' => 0,
                'type' => [0,1,2]
                ])
            ->limit(12);
        }
        $raiseds = $query->orderBy('raised_id DESC')
        ->offset(4*($page-1))
        ->limit(4)
        ->all();
        //匹配用户id和用户名，计算进度
        foreach ($raiseds as $key => $raised)
        {
            $query1 = Uc_user::findOne([
                'user_id' => $raised->leader_id
                ]);
            $query2 = Uc_user::findOne([
                'user_id' => $raised->referrer_id
                ]);
            if(null == $query1)
            {
             $raised->leader_name = "暂无";
         }
         else
         {
             $raised->leader_name = $query1->user_name;
         }
         if(null == $query2)
         {
             $raised->referrer_name = "暂无";
         }
         else
         {
             $raised->referrer_name = $query2->user_name;
         }
            //进度百分比，保留2位小数
         $progress = $raised->raisedMoney/$raised->totalMoney*100;
         $raised->progress = round($progress, 2);
         $raised->days = floor((strtotime($raised->expire)-time())/86400);
     }
     $str1 = Json::encode($raiseds);
     $str2 = json_encode($raiseds);
     $arr1 = json_decode($str1);
     $arr2 = json_decode($str2);
     for($i = 0; $i < 4; $i++)
     {
        foreach($arr2[$i] as $key => $value)
        {
            $arr1[$i]->$key = $value;
        }
     }
     return Json::encode($arr1);
 }

}